<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- 自定义表单验证
          validator:(rule,value,callback)=>{
              rule:当前的验证规则
              value:当前验证项的值
              callback:如果验证允许通过  callback()  不能通过callback(new Error("错误信息"))
              注意点:任何if判断 都需要最后有一个callback
          }
        
        -->
      <el-form :model="form" label-width="80px" :rules="rules">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <script src="./vue.js"></script>
    <link rel="stylesheet" href="./ele.css" />
    <script src="./index.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          form: {
            email: "",
            phone: "",
          },
          rules: {
            email: [
              { required: true, message: "请输入邮箱", trigger: "change" },
              {
                validator: (rule, value, callback) => {
                  //   window.console.log("rule", rule);
                  //   window.console.log("value", value);
                  //   window.console.log("callback", callback);
                  //   value:当前验证项的值
                  let reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
                  if (reg.test(value)) {
                    //   正则验证成功,正常通过
                    callback();
                  } else {
                    //   验证不成功
                    callback(new Error("请正确输入邮箱地址"));
                  }
                },
                trigger: "change",
              },
            ],
            phone: [
              { required: true, message: "请输入手机号", trigger: "change" },
              {
                validator: (rule, value, callback) => {
                  let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
                  if (reg.test(value)) {
                    callback();
                  } else {
                    callback(new Error("请正确输入手机号"));
                  }
                },
                trigger: "change",
              },
            ],
          },
        },
      });
    </script>
  </body>
</html>
